package com.lc.pianostudent.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lc.pianostudent.R;
import com.lc.pianostudent.activity.CircleImageActivity;
import com.lc.pianostudent.bean.PianoIndexBean;
import com.lc.pianostudent.bean.PianoTimeBean;
import com.zcx.helper.glide.GlideLoader;
import com.zcx.helper.scale.ScaleScreenHelperFactory;

import java.util.ArrayList;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by Administrator on 2018/6/25.
 */

public class PianoIndexAdapter extends RecyclerView.Adapter<PianoIndexAdapter.ViewHolder>{


    private Context context;

    private List<PianoIndexBean> list = new ArrayList<>();

    public PianoIndexAdapter(Context context, List<PianoIndexBean> list) {
        this.context = context;
        this.list = list;
    }

    public interface OnItemClickListener {

        void onItemClick(View view, int position);

    }

    private PianoIndexAdapter.OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(PianoIndexAdapter.OnItemClickListener listener) {

        this.onItemClickListener = listener;

    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_piano_index, parent, false);

        ScaleScreenHelperFactory.getInstance().loadViewGroup((ViewGroup) view);

        PianoIndexAdapter.ViewHolder viewHolder = new PianoIndexAdapter.ViewHolder(view);

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {

        if(list.get(position).getColor().equals("black")){
            holder.itemview_bg.setBackgroundResource(R.mipmap.ph_line1);
        }else if(list.get(position).getColor().equals("red")){
            holder.itemview_bg.setBackgroundResource(R.mipmap.ph_line2);

        }
        holder.name.setText(list.get(position).getUserid());

        holder.index.setText(list.get(position).getNumber());

        holder.order.setText(list.get(position).getOrder());

        GlideLoader.getInstance().get(this,list.get(position).getAvatar() , holder.head_image,R.mipmap.new_head);

        if(list.get(position).getOrder().equals("1")){
            GlideLoader.getInstance().get(this,"" , holder.head_bg,R.mipmap.ph_jin);
        }else if(list.get(position).getOrder().equals("2")){
            GlideLoader.getInstance().get(this,"" , holder.head_bg,R.mipmap.ph_yin);
        }else if(list.get(position).getOrder().equals("3")){
            GlideLoader.getInstance().get(this,"" , holder.head_bg,R.mipmap.ph_tong);
        }else {
            holder.head_bg.setVisibility(View.INVISIBLE);
        }





        setUpItemEvent(holder,position);

    }

    //为RecyclerView添加点击事件
    protected void setUpItemEvent(final PianoIndexAdapter.ViewHolder holder, final  int position) {

        if (onItemClickListener != null) {

            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    //  int LayoutPosition = holder.getLayoutPosition();

                    onItemClickListener.onItemClick(holder.itemView, position);



                }

            });

        }

    }
    @Override
    public int getItemCount() {
        return list.size();
    }


    //初始化组件
    public static class ViewHolder extends RecyclerView.ViewHolder {

        public TextView name,index,order;
        public CircleImageView head_image;
        public ImageView head_bg;
        public RelativeLayout itemview_bg;

        public ViewHolder(View itemView) {
            super(itemView);

            name = (TextView) itemView.findViewById(R.id.name);
            index = (TextView) itemView.findViewById(R.id.index);
            head_image = itemView.findViewById(R.id.head_image);
            order = itemView.findViewById(R.id.order);
            head_bg = itemView.findViewById(R.id.head_bg);
            itemview_bg = itemView.findViewById(R.id.itemview_bg);
        }
    }
}
